
//
//  LVPhotoBrowserViewController.swift
//  PhotoBrowser
//
//  Created by 刘伟 on 16/4/28.
//  Copyright © 2016年 刘伟. All rights reserved.
//

import UIKit

let photoBrowserCellID = "photoBrowserCell"
class LVPhotoBrowserViewController: UIViewController {
    
    var index : NSIndexPath?
    var shops : [LVShop]?
    
    lazy var collectionView : UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: LVPhotoBrowserFlowLayout())
    lazy var closeBtn : UIButton = UIButton()
    lazy var saveBtn : UIButton = UIButton()
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
    //  1.添加子控件
        addChildWidth()

    //       一开始呈现的就是点击的那张图片
        guard let indexPath = index else {return}
        collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false)
    }
    
    
    override func loadView() {
        super.loadView()
        view.frame.size.width += 15
    }
    
    
    
}


// MARK: - 添加子控件
extension LVPhotoBrowserViewController{
    
    func addChildWidth(){
        
        closeBtn.setTitle("关闭", forState: .Normal)
        closeBtn.backgroundColor = UIColor.darkGrayColor()
        closeBtn.addTarget(self, action: "closeBtnClick", forControlEvents: .TouchUpInside)
        
        saveBtn.setTitle("保存", forState: .Normal)
        saveBtn.backgroundColor = UIColor.darkGrayColor()
        saveBtn.addTarget(self, action: "saveBtnClick", forControlEvents: .TouchUpInside)
        
        collectionView.frame = view.bounds
        let margin : CGFloat = 15.0
        let btnW : CGFloat = 90.0
        let btnH : CGFloat = 32.0
        let y = UIScreen.mainScreen().bounds.height - btnH - margin
        closeBtn.frame = CGRect(x: margin, y: y, width: btnW, height: btnH)
        let x = UIScreen.mainScreen().bounds.width - btnW - margin
        saveBtn.frame = CGRect(x: x, y: y, width: btnW, height: btnH)
        
        self.view.addSubview(collectionView)
        self.view.addSubview(closeBtn)
        self.view.addSubview(saveBtn)
        
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.registerClass(LVPhotoBrowserCollectionViewCell.self, forCellWithReuseIdentifier: photoBrowserCellID)
    }
}


// MARK: - 按钮点击
extension LVPhotoBrowserViewController{
    
    func closeBtnClick(){
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    func saveBtnClick(){
        
//        1.取出屏幕中的cell
        let cell = collectionView.visibleCells().first as? LVPhotoBrowserCollectionViewCell
        
//        2.取出cell中的图片
        guard let image = cell?.imageV.image else{return}
        
//        3.将图片写入相册
        UIImageWriteToSavedPhotosAlbum(image, nil,nil, nil)
    }
}


// MARK: - collectionV的数据源方法
extension LVPhotoBrowserViewController : UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        //        ??一般用在可选链后面
        //        1.如果可选链中有一个为空那么直接用??后面的值返回
        //        2.如果都不为空,那么对最终的进行强制解包
        return shops?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        //        1.创建cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(photoBrowserCellID, forIndexPath: indexPath) as! LVPhotoBrowserCollectionViewCell
        cell.shop = shops?[indexPath.item]
        
        return cell
        
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        closeBtnClick()
    }
    
}


